(function() {
    function ajax(url, data, type) {
        return new Promise(function(resolve, reject) {
            var defaultJson = {
                url: url,
                type: type || "GET",
                data: data,
                dataType: "json",
                success: function(res) {
                    var status = res.head.status;
                    if (status === "Y") {
                        resolve(res.body);
                    } else {
                        var msg = res.head.msg;
                        console.log("出错信息", msg);
                        reject(msg);
                    }
                },
                error: function() {
                    console.log("请求出错！");
                }
            }
            var postJson = {
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data)
            }

            if (type === "POST") {
                $.extend(defaultJson, postJson);
            }
            $.ajax(defaultJson);
        })
    }

    var prefix = "/lucifer-deploy/leadership";

    var urlMap = {
        getAuthInfo: prefix + "/leader/getAuthInfo",   // 授权
        addPresentee: prefix + "/leader/addPresentee",  // 新增被推荐者信息
        getInfluenceListByPage: prefix + "/leader/getInfluenceListByPage",  // 获取影响力排行榜
        getLeaderInfluence: prefix + "/leader/getLeaderInfluence", // 获取本人的影响力信息
        getShareConfig: prefix + "/leader/getShareConfig",  // 获取分享配置信息
        getLeaderInfo: prefix + "/presentee/getLeaderInfo",  // 获取传播者信息
        save: prefix + "/presentee/save"  // 新增被推荐者信息
    };

    var mockPrefix = "../__mock__";

    var mockUrlMap = {
        getAuthInfo: mockPrefix + "/getAuthInfo.json",
        addPresentee: mockPrefix + "/addPresentee.json",
        getInfluenceListByPage: mockPrefix + "/getInfluenceListByPage.json",
        getLeaderInfluence: mockPrefix + "/getLeaderInfluence.json",
        getShareConfig: mockPrefix + "/getShareConfig.json",
        getLeaderInfo: mockPrefix + "/getLeaderInfo.json",
        save: mockPrefix + "/save.json"
    };

    var mock = false;

    if (mock) {
        urlMap = mockUrlMap;
    }

    var API = {
        "getAuthInfo": function() {
            return ajax(urlMap.getAuthInfo);
        },
        "addPresentee": function(data) {
            return ajax(urlMap.addPresentee, data, "POST");
        },
        "getInfluenceListByPage": function(data) {
            return ajax(urlMap.getInfluenceListByPage, data);
        },
        "getLeaderInfluence": function() {
            return ajax(urlMap.getLeaderInfluence);
        },
        "getShareConfig": function(data) {
            data.isWxAuth = data.isWxAuth || false;
            return ajax(urlMap.getShareConfig, data);
        },
        "getLeaderInfo": function(data) {
            return ajax(urlMap.getLeaderInfo, data);
        },
        "save": function(data) {
            return ajax(urlMap.save, data, "POST");
        }
    };

    window.API = API;
})();